package hu;


import java.util.*;

public class hu68 {
    static class people{
        String name;
        int score;
        int t;

        public people(String name, int score, int t) {
            this.name = name;
            this.score = score;
            this.t = t;
        }

    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.nextLine();
        int order = sc.nextInt();
        sc.nextLine();
        PriorityQueue<people> queue;
        if (order == 0) {
            queue = new PriorityQueue<>((a, b) -> {
                if(b.score == a.score)
                    return a.t - b.t;
                return b.score - a.score;
            });
        }
        else{
            queue = new PriorityQueue<>((a, b) -> {
                if(a.score == b.score)
                    return a.t - b.t;
                return a.score - b.score;
            });
        }
        for (int i = 0; i < n; i++) {
            String[] s = sc.nextLine().split(" ");
            people p = new people(s[0], Integer.parseInt(s[1]), i);
            queue.add(p);
        }
        for (int i = 0; i < n; i++) {
            people p = queue.poll();
            System.out.println(p.name + " " + p.score);
        }

    }
}
/*
3
0
fang 90
yang 50
ning 50
 */